JAVA 您所在的位置:网站首页 java 图片旋转 JAVA

JAVA

2023-08-04 05:05| 来源: 网络整理| 查看: 265

base64 图片旋转实现

public final class Base64ImageUtil { private static BufferedImage rotate(Image src, int angel) { int src_width = src.getWidth(null); int src_height = src.getHeight(null); // calculate the new image size Rectangle rect_des = CalcRotatedSize(new Rectangle(new Dimension( src_width, src_height)), angel); BufferedImage res = null; res = new BufferedImage(rect_des.width, rect_des.height, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = res.createGraphics(); res = g2.getDeviceConfiguration().createCompatibleImage(rect_des.width, rect_des.height, Transparency.TRANSLUCENT); g2.dispose(); g2 = res.createGraphics(); // transform g2.translate((rect_des.width - src_width) / 2, (rect_des.height - src_height) / 2); g2.rotate(Math.toRadians(angel), src_width / 2, src_height / 2); g2.setColor(new Color(255,0,0)); g2.setStroke(new BasicStroke(1)); g2.drawImage(src, null, null); g2.dispose(); return res; } public static Rectangle CalcRotatedSize(Rectangle src, int angel) { // if angel is greater than 90 degree, we need to do some conversion if (angel >= 90) { if(angel / 90 % 2 == 1){ int temp = src.height; src.height = src.width; src.width = temp; } angel = angel % 90; } double r = Math.sqrt(src.height * src.height + src.width * src.width) / 2; double len = 2 * Math.sin(Math.toRadians(angel) / 2) * r; double angel_alpha = (Math.PI - Math.toRadians(angel)) / 2; double angel_dalta_width = Math.atan((double) src.height / src.width); double angel_dalta_height = Math.atan((double) src.width / src.height); int len_dalta_width = (int) (len * Math.cos(Math.PI - angel_alpha - angel_dalta_width)); int len_dalta_height = (int) (len * Math.cos(Math.PI - angel_alpha - angel_dalta_height)); int des_width = src.width + len_dalta_width * 2; int des_height = src.height + len_dalta_height * 2; return new Rectangle(new Dimension(des_width, des_height)); } /** * 顺时针旋转 * @param base64FileStr * @param rotate * @return * @throws IOException */ public static String rotateImage(String base64FileStr,int rotate) throws IOException { if(StringUtils.isEmpty(base64FileStr)){ return ""; } Base64.Decoder decoder = Base64.getDecoder(); // Base64解码,对字节数组字符串进行Base64解码并生成文件 final byte[] byt = decoder.decode(base64FileStr); for (int i = 0, len = byt.length; i < len; ++i) { // 调整异常数据 if (byt[i] < 0) { byt[i] += 256; } } InputStream input = new ByteArrayInputStream(byt); ByteArrayOutputStream out = new ByteArrayOutputStream(); BufferedImage src = ImageIO.read(input); BufferedImage des = rotate(src, rotate); ImageIO.write(des,"png",out); final Base64.Encoder encoder = Base64.getEncoder(); final byte[] bytes = out.toByteArray(); return encoder.encodeToString(bytes); } public static void main(String[] args) throws IOException { String base64FileStr = "iVBORw0KGgoAAAANSUhEUgAAAGQAAADnCAYAAADoxvMiAAAAAXNSR0IArs4c6QAACC9JREFUeF7tmk+IG1Ucx99vJk0Xu664bHfyRotIpSJI8SAqFv8gomARxB7q0auCXjyKongRPIiKvffgQamIf1ChhyLWogcPHkREhSLdN5N0d6ulZd3sZJ68kFlmhyQ7k+zqN+k3p7aZmXzf5zPf9+ZPRfEDRUCg0jCMohCwk4BCKASMAFgcNoRCwAiAxWFDKASMAFgcNoRCwAiAxWFDKASMAFgcNoRCwAiAxWFDKASMAFgcNoRCwAiAxWFDKASMAFgcNoRCwAiAxZnYhoRhaB1LY8zEjqHfuTCRg9FafyIiT7kBWWvTKIp8sBN95DgTKcSNNmuItVaJyNQ0ZWKFOClaa+tkZFLSNH09juPXRj49AXacaCE9KWdE5OEcS2OMuWmn2GZN7B3vZ2utb619M47jkzv1G/njTLyQ3mBmwjBcywa2k+tKcWocIMFdYKxtbGwcvHjxYjyOqGkR0mWQTWEZEGOM59b9cQDlhbjjuCmydzGx+efC39vGmL2j/uZUCekt9qnjlpMy1hgzIUmSHGm1WufccYMg2Od53nml1F4Rmc3/3riX4mOFHfUs2O39tNbHRORU7nfWjTEzo/xuriFXoii6fpRjVNlnKoU4AI1G4x7P837IrSs2iiI3hVX65Bb1DWNMvdLOI2w8tUIyFlrrRES6N47u8lgpVelG8poVorVOJVsxh5xZ1tqWUqqZpunLzWbz8zInodb6rIgcybXF/fF8mqZ3NpvNq8OOsZ2QMAxP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wduN48ILSdP0jTiOX80PPgzDc51O58Nms9l95jVNn0kQ8kQcx19NE/RhY4EVcq0IKI6TQsDMUwiFgBEAi8OGUAgYAbA4bAiFgBEAi8OGUAgYAbA4bAiFgBEAi8OGUAgYAbA4bAiFgBEAi8OGUAgYAbA4bAiFgBEAi8OGUAgYAbA4bAiFgBEAi8OGUAgYAbA4bAiFgBEAi8OGUAgYAbA4bAiFgBEAi8OGUAgYAbA4bAiFgBEAi8OGUAgYAbA4bAiFgBEAi8OGUAgYAbA4bAiFgBEAi8OGUAgYAbA4bAiFgBEAi8OGUAgYAbA4bAiFgBEAi8OGUAgYAbA4bAiFgBEAi8OGUAgYAbA4bAiFgBEAi8OGUAgYAbA4bAiFgBEAi8OGUAgYAbA4bAiFgBEAi8OGUAgYAbA4bAiFgBEAi8OGUAgYAbA4bAiFgBEAi8OGUAgYAbA4bAiFgBEAi8OGUAgYAbA4bAiFgBEAi8OGUAgYAbA4bAiFgBEAi8OGUAgYAbA4bAiFgBEAi8OGUAgYAbA4bAiFgBEAi8OGUAgYAbA4bAiFgBEAi8OGUAgYAbA4bAiFgBEAi8OGUAgYAbA4bAiFgBEAi8OGUAgYAbA4bAiFgBEAi8OGUAgYAbA4bAiFgBEAi8OGUAgYAbA4bAiFgBEAi8OGUAgYAbA4bAiFgBEAi8OGUAgYAbA4bAiFgBEAi8OGUAgYAbA4bAiFgBEAi8OGUAgYAbA4bAiFgBEAi8OGUAgYAbA4bAiFgBEAi8OGUAgYAbA4bAiFgBEAi8OGUAgYAbA4bAiFgBEAi8OGUAgYAbA4bAiFgBEAi8OGUAgYAbA4bAiFgBEAi8OGUAgYAbA4bAiFgBEAi8OGUAgYAbA4bAiFgBEAi8OGUAgYAbA4bAiFgBEAi8OGUAgYAbA4bAiFgBEAi8OGUAgYAbA4bAiFgBEAi/MvstaUBhPOZRQAAAAASUVORK5CYII="; final String image = rotateImage(base64FileStr, 270); System.out.println(image); } }


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有